#include "FireworkManager.h"

#include "DataSingleton.h"
#include "Firework.h"

void FireworkManager::setFireworkCount(uint8_t fireworkCount)
{
    m_fireworkCount = fireworkCount;
    std::uniform_real_distribution<double> posDis{-10, 50};
    std::uniform_real_distribution<double> speedDis{30, 40};
    auto &gen = DataSingleton::instance()->m_gen;
    for (int i = 0; i < m_fireworkCount; ++i)
    {
    vsg::dvec3 pos{posDis(gen), posDis(gen), posDis(gen)};
    double speed{speedDis(gen)};
    uint32_t particleCount = 500;
    auto firework = std::make_shared<Firework>(pos, speed, particleCount);
    firework->init();
    firework->activate();
    m_fireworks.push_back(firework);
    }
}

void FireworkManager::update(std::chrono::nanoseconds delt)
{
    for (auto &firework : m_fireworks)
    {
        firework->update(delt);
        if (firework->isDone())
        {
            std::uniform_real_distribution<double> posDis{-10, 50};
            std::uniform_real_distribution<double> speedDis{30, 40};
            auto &gen = DataSingleton::instance()->m_gen;
            vsg::dvec3 pos{posDis(gen), posDis(gen), 0.};
            double speed{speedDis(gen)};
            firework->reset(pos, speed);
            firework->activate();
        }
    }
}

vsg::ref_ptr<vsg::Node> FireworkManager::getNode()
{
    auto scene = vsg::Group::create();
    for (auto firework : m_fireworks)
    {
    scene->addChild(firework->getNode());
    }
    return scene;
}
